package cn.kgc.runner;

import cn.kgc.base.BaseInfoProperties;
import cn.kgc.entity.Industry;
import cn.kgc.service.IndustryService;
import org.redisson.api.RBloomFilter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangyue
 * @version 1.0
 * 项目启动时缓存预热
 */
@Component
@SuppressWarnings("all")
public class CachePreheatRunner extends BaseInfoProperties implements CommandLineRunner {
    @Resource
    private IndustryService industryService;

    @Resource
    private RBloomFilter industryRBloomFilter;

    @Override
    public void run(String... args){
         //1 将所有一级行业的信息保存到缓存中
        List<Industry> topIndustry = industryService.getTopIndustry();
        redisUtil.set(INDUSTRY_ALL_PREFIX,topIndustry);

        topIndustry.forEach(industry -> {
            //2 将一级行业信息下的三级行业信息保存到缓存中
             List<Industry> thirdIndustry= industryService.getThirdIndustry(industry.getId());
             String key= INDUSTRY_TOP_PREFIX+industry.getId();
             redisUtil.set(key,thirdIndustry);

             //项目启动时将一级行业的id保存到布隆过滤器中
             industryRBloomFilter.add(industry.getId());
        });
    }
}